package cn.spike.user.controller;

import cn.spike.basic.exception.BusinessException;
import cn.spike.basic.util.AjaxResult;
import cn.spike.user.dto.LoginDto;
import cn.spike.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setObj(map);
        } catch (BusinessException e) {
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试【400，404，405】");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String, String> map) {
        try {
            //登录过 - 返回token和登录信息 ;  第一次扫码登陆 - 返回false和access_token和openId
            //所以直接在业务中返回AjaxResult
            return logininfoService.wechatLogin(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试【400，404，405】");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String, String> map) {
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试【400，404，405】");
        }
    }

}
